package logic.ships.drone;

import map.asteroids.ResourceAsteroid;
import ai.AIController;

public class DroneController extends AIController {
	
	private static final long serialVersionUID = 1L;
	
	private Drone drone;
	
	public DroneController(Drone drone) {
		super(drone);
		this.drone = drone;
	}
	
	@Override
	public void delayedUpdate(float time) {
		if(drone.getTargetLocation() == null) return;
		
		float targetDist = drone.getTargetLocation().distance(drone.getLocalTranslation());
		boolean rotate = true;
		
		if(drone.isHarvesting()) {
			if(targetDist <= drone.getCurrentAsteroid().getSize() * 3.0f) {
				rotate = false;
				harvestAsteroid(time);
			}
		} else {
			if(targetDist <= 30f) {
				rotate = false;
				dropLoad(time);
			}
		}
		
		if(rotate) rotateTo(time, drone.getTargetLocation());
		
		drone.getMoveForwardController().stop(!rotate);
	}
	
	public void harvestAsteroid(float time) {
		if(drone.getAsteroid().isEmpty()) return;
		
		float harvestAmount = drone.getHarvestSpeed() * time;
		if(harvestAmount > drone.getAsteroid().getCurrentResources()) 
			harvestAmount = drone.getAsteroid().getCurrentResources();
		drone.harvest(harvestAmount);
		
		if(drone.isFullyLoaded()) drone.drop();
	}
	
	public void dropLoad(float time) {
		float dropAmount = drone.getDropSpeed() * time;
		drone.drop(dropAmount);
		if(drone.loadIsEmpty()) {
			ResourceAsteroid a = drone.getCurrentAsteroid();
			if(!a.isEmpty()) drone.harvestAsteroid(drone.getCurrentAsteroid());
			else drone.harvestAsteroid(null);
		}
	}
}